﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuteEditor;
using Wjtour.Domain;
using Wjtour.BLL;
using Hjp.FrameWork.Common;
using System.IO;

namespace Wjtour.Web.Admin.picture
{
    public partial class addedi : BasePage
    {
        protected DropDownList cid;
        protected TextBox title;
        protected TextBox img;
        protected TextBox picturedesc;
        protected Button submit;
        protected FileUpload upload;
        protected Button btnPublish;
        protected Image imgPreview;
        protected string type;
        protected HiddenField bigImg;
        protected CuteWebUI.Uploader Uploader1;
        protected Button button1;
        protected TextBox rar;

        protected int id = 0;
        Wjtour.Domain.sitepicture link = new Domain.sitepicture();
        sitepicture_bll _bll = new sitepicture_bll();
        picturetype_bll _tbll = new picturetype_bll();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            type = Request["type"];
            submit.Click += new EventHandler(submit_Click);
            btnPublish.Click += btnPublish_Click;
            Uploader1.FileUploaded += Uploader1_FileUploaded;

            if (!IsPostBack)
            {
                BindDropdown();
            }
            if (id > 0)
            {
                link = _bll.Load(id);
                if (!IsPostBack)
                {
                    if (link != null)
                    {
                        title.Text = link.picturetitle;
                        cid.SelectedValue = link.picturetypeid.ToString();
                        img.Text = link.picturepath;
                        imgPreview.ImageUrl = link.smallImg;
                        picturedesc.Text = link.picturedesc;
                        rar.Text = link.rar;
                    }
                }
            }
        }

        void Uploader1_FileUploaded(object sender, CuteWebUI.UploaderEventArgs args)
        {
            string rarpath = Server.MapPath("~/uploads/rar/");
            if (!Directory.Exists(rarpath))
                Directory.CreateDirectory(rarpath);

            string filepath = "~/uploads/rar/" + args.FileName;
            string filePhysicalPath = Server.MapPath(filepath);
            if (File.Exists(filePhysicalPath))
                File.Delete(filePhysicalPath);
            args.CopyTo("~/uploads/rar/" + args.FileName);
            args.Delete();
            rar.Text = "uploads/rar/" + args.FileName;
        }

        void btnPublish_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(upload.FileName))
            {
                Utils.Alert(this.Page, "请选择要上传的文件！");
                return;
            }
            string fileExt = Path.GetExtension(upload.FileName);
            List<string> allowExt = new List<string>();
            allowExt.Add(".jpg");
            allowExt.Add(".png");
            allowExt.Add(".gif");

            if (!allowExt.Contains(fileExt))
            {
                Utils.Alert(this.Page, "系统只允许上传jpg,jpeg,gif的文件！");
                return;
            }
            string uploadDir = Server.MapPath("/uploads");
            if (!Directory.Exists(uploadDir))
            {
                Directory.CreateDirectory(uploadDir);
            }

            string guid = GetFileName("time");
            string filename = guid + fileExt;
            string path = uploadDir + "\\" + filename;
            if (System.IO.File.Exists(filename)) System.IO.File.Delete(path);

            try
            {
                upload.SaveAs(path);
                bigImg.Value = "/uploads/" + filename;
                string smallImg = ImageUtil.ConvertImageToProperSize(HttpContext.Current, "/uploads/" + filename, 156, 106, "jpg");
                imgPreview.ImageUrl = smallImg;
                img.Text = bigImg.Value;
            }
            catch (Exception ex)
            {
                Utils.Alert(this.Page, "文件文件出错，文件大小超过限制或网络原因！");
                return;
            }
        }

        void BindDropdown()
        {
            string sql = "select * from picturetype";
            if (!string.IsNullOrEmpty(type))
                sql = "select * from picturetype where id in (" + type + ")";

            List<picturetype> types = _tbll.GetAll<picturetype>(sql).ToList();
            if (types != null && types.Count > 0)
            {
                List<picturetype> ptypes = types.Where(t => t.parenttypeid == 0).ToList();
                foreach (var ptype in ptypes)
                {
                    cid.Items.Add(new ListItem(ptype.picturetypename, ptype.id.ToString()));
                    List<picturetype> subtypes = types.Where(t => t.parenttypeid == ptype.id).ToList();
                    if (subtypes != null && subtypes.Count > 0)
                    {
                        foreach (var item in subtypes)
                        {
                            cid.Items.Add(new ListItem(" ┝ " + item.picturetypename, item.id.ToString()));
                        }
                    }
                }
            }
        }

        void submit_Click(object sender, EventArgs e)
        {
            link.id = id;
            link.picturetitle = title.Text;
            link.picturetypeid = Utils.StrToInt(cid.SelectedValue, 0);
            link.picturepath = img.Text;
            link.smallImg = imgPreview.ImageUrl;
            link.picturedesc = picturedesc.Text;
            link.rar = rar.Text;
            link.isaudit = 2;

            if (link.id == 0)
                link.createdate = DateTime.Now;

            bool b = _bll.Save(link);
            if (b)
            {
                SaveLog(id == 0 ? "Add" : "edit");
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "list.aspx?type=" + type, false);
            }
        }
    }
}